package com.hnkywl.sts.dao.wlgl;

import com.hnkywl.sts.entity.organ.Ysgj;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-6-4
 * Time: 上午12:29
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class ZjglDao extends SimpleHibernateTemplate<Ysgj,Long> {
   /* public Page<Ysgj> findZjglList(String wlsmc,Long wlsId,String ysgjlx ,Long ysgjlxId,int pageNo, int pageSize) {
        Page<Ysgj> page = new Page<Ysgj>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("dateCreated");
        if (StringUtils.isNotEmpty(wlsmc)) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.like("wls.qc", wlsmc, MatchMode.START)});
        }else if (wlsId != null) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.eq("wls.id", wlsId)});
        }  else   if (StringUtils.isNotEmpty(ysgjlx)) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.like("ysgjlx.z", ysgjlx, MatchMode.START)});
        }else if (ysgjlxId != null) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.eq("ysgjlx.id", ysgjlxId)});
        }  else {
            return this.findAll(page);
        }
    }
*/

    public Page<Ysgj> findZjglList(String wlsmc,Long wlsId,String ysgjlx ,Long ysgjlxId,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Ysgj> page = new Page<Ysgj>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (wlsId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" wls.id = ? ");
            param.add(wlsId);
        }
        if (wlsmc != null && !wlsmc.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" wls.qc like ? ");
            param.add("%" + wlsmc.trim() + "%");
        }
        if (ysgjlx != null&&!ysgjlx.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" ysgjlx.z like ? ");
            param.add("%" + ysgjlx.trim() + "%");
        }
        if (ysgjlxId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" ysgjlx.id = ? ");
            param.add(ysgjlxId);
        }

        //==============记录数量===============
        hql = "select count(*) from Ysgj ";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Ysgj ";
        if (condition.length() != 0)
            hql = hql + " where " + condition + "order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }





    public boolean checkQc(Long id,String ysgjsbm) {
        if (id == null){
            //新增校验
            String hql = "from Ysgj where ysgjsbm = ?";
            return existsQuery(hql,ysgjsbm);
        } else {
            //修改校验
            String hql = "from Ysgj where id<>? and ysgjsbm = ?";
            return existsQuery(hql,id,ysgjsbm);
        }
    }
}
